home *** CD-ROM | disk | FTP | other *** search
/ PC go! 2008 April / PCgo 2008-04 (DVD).iso / interface / contents / demoversionen_3846 / 13664 / files / Data1.cab / eventdlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-11-20  |  10.7 KB  |  443 lines

  1. /******************************************************************/
  2. /*                                                                */
  3. /*                      TurboCAD for Windows                      */
  4. /*                   Copyright (c) 1993 - 2001                    */
  5. /*             International Microcomputer Software, Inc.         */
  6. /*                            (IMSI)                              */
  7. /*                      All rights reserved.                      */
  8. /*                                                                */
  9. /******************************************************************/
  10.  
  11. // EventDlg.cpp : implementation file
  12. //
  13.  
  14. #include "stdafx.h"
  15. #include "Events.h"
  16. #include "EventDlg.h"
  17.  
  18. #ifdef _DEBUG
  19. #define new DEBUG_NEW
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23.  
  24. extern CTCEventsApp theApp;
  25.  
  26. static void DDX_EventMask(CDataExchange* pDX, UINT nIDCFirst, UINT nIDCLast, DWORD& value)
  27. {
  28.     HWND hWndDlg = pDX->m_pDlgWnd->m_hWnd;
  29.     DWORD dwBit = 1;
  30.     UINT nID;
  31.     HWND hWndButton;
  32.     if (pDX->m_bSaveAndValidate)
  33.     {
  34.         value = 0;
  35.         for (nID = nIDCFirst; nID <= nIDCLast; ++nID, dwBit <<= 1)
  36.         {
  37.             hWndButton = ::GetDlgItem(hWndDlg, nID);
  38.             if (::SendMessage(hWndButton, BM_GETCHECK, 0, 0) != 0)
  39.                 value |= dwBit;
  40.         }
  41.     }
  42.     else
  43.     {
  44.         for (nID = nIDCFirst; nID <= nIDCLast; ++nID, dwBit <<= 1)
  45.         {
  46.             hWndButton = ::GetDlgItem(hWndDlg, nID);
  47.             ::SendMessage(hWndButton, BM_SETCHECK, (value & dwBit) ? 1 : 0, 0);
  48.         }
  49.     }
  50. }
  51.  
  52. /////////////////////////////////////////////////////////////////////////////
  53. // CAboutDlg dialog used for App About
  54.  
  55. class CAboutDlg : public CDialog
  56. {
  57. public:
  58.     CAboutDlg();
  59.  
  60. // Dialog Data
  61.     //{{AFX_DATA(CAboutDlg)
  62.     enum { IDD = IDD_ABOUTBOX };
  63.     //}}AFX_DATA
  64.  
  65.     // ClassWizard generated virtual function overrides
  66.     //{{AFX_VIRTUAL(CAboutDlg)
  67.     protected:
  68.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  69.     //}}AFX_VIRTUAL
  70.  
  71. // Implementation
  72. protected:
  73.     //{{AFX_MSG(CAboutDlg)
  74.     //}}AFX_MSG
  75.     DECLARE_MESSAGE_MAP()
  76. };
  77.  
  78. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  79. {
  80.     //{{AFX_DATA_INIT(CAboutDlg)
  81.     //}}AFX_DATA_INIT
  82. }
  83.  
  84. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  85. {
  86.     CDialog::DoDataExchange(pDX);
  87.     //{{AFX_DATA_MAP(CAboutDlg)
  88.     //}}AFX_DATA_MAP
  89. }
  90.  
  91. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  92.     //{{AFX_MSG_MAP(CAboutDlg)
  93.         // No message handlers
  94.     //}}AFX_MSG_MAP
  95. END_MESSAGE_MAP()
  96.  
  97. /////////////////////////////////////////////////////////////////////////////
  98. // CTCEventsDlg dialog
  99.  
  100. CTCEventsDlg::CTCEventsDlg(CWnd* pParent /*=NULL*/)
  101.     : CDialog(CTCEventsDlg::IDD, pParent),
  102.     m_dwEventConnection(0),
  103.     m_dwEventMask(~0UL),
  104.     m_pIApplication(NULL)
  105. {
  106.     //{{AFX_DATA_INIT(CTCEventsDlg)
  107.     //}}AFX_DATA_INIT
  108.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  109.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  110. }
  111.  
  112. // TurboCAD SDK: make sure we release the application
  113. CTCEventsDlg::~CTCEventsDlg()
  114. {
  115.     ReleaseApp();
  116. }
  117.  
  118. void CTCEventsDlg::DoDataExchange(CDataExchange* pDX)
  119. {
  120.     CDialog::DoDataExchange(pDX);
  121.     //{{AFX_DATA_MAP(CTCEventsDlg)
  122.     //}}AFX_DATA_MAP
  123.     DDX_EventMask(pDX, IDC_EVENT1, IDC_EVENT28, m_dwEventMask);
  124. }
  125.  
  126. // Public function to allow event hander to post a message on our edit box
  127.  
  128. void CTCEventsDlg::AppendText(LPCTSTR lpszText)
  129. {
  130.     int nStart, nEnd;
  131.     m_edit.SetSel(0, -1);
  132.     m_edit.GetSel(nStart, nEnd);
  133.     if (nEnd > 4000)
  134.     {
  135.         // Dump the buffer if it's too big
  136.         Clear();
  137.         nEnd = 0;
  138.     }
  139.     m_edit.SetSel(nEnd, nEnd);
  140.     m_edit.ReplaceSel(lpszText);
  141. }
  142.  
  143. // Clear our edit box
  144.  
  145. void CTCEventsDlg::Clear()
  146. {
  147.     m_edit.SetSel(0, -1);
  148.     m_edit.Clear();
  149. }
  150.  
  151. // Create an instance of the TurboCAD local server object
  152.  
  153. IUnknown* CTCEventsDlg::CreateLocalServerObject()
  154. {
  155.     // Try to create instance from TurboCAD
  156.     CString strStr;
  157.     IUnknown* pUnk = NULL;
  158.     HRESULT hRes = ::CoCreateInstance(CLSID_Application, NULL,
  159.         CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pUnk);
  160.  
  161.     if (FAILED(hRes))
  162.     {
  163.         strStr.LoadString(IDS_COULDNOT_CREATETCADAPP);
  164.         AfxMessageBox(strStr);
  165.         return NULL;
  166.     }
  167.     return pUnk;
  168. }
  169.  
  170. // Get the running instance of the TurboCAD local server object from the ROT.
  171.  
  172. IUnknown* CTCEventsDlg::GetActiveObject(BOOL* pbCreateInstance)
  173. {
  174.     // Try to connect to running TurboCAD
  175.     IUnknown* pUnk = NULL;
  176.     CString strStr;
  177.     HRESULT hRes = ::GetActiveObject(CLSID_Application, NULL, &pUnk);
  178.     if (SUCCEEDED(hRes))
  179.     {
  180.         // Did not create instance
  181.         *pbCreateInstance = FALSE;
  182.         return pUnk;
  183.     }
  184.     if (!*pbCreateInstance)
  185.     {
  186.         strStr.LoadString(IDS_TURBOCAD_ISNOT_ACTIVE);
  187.         AfxMessageBox(strStr);
  188.         return NULL;
  189.     }
  190.     return CreateLocalServerObject();
  191. }
  192.  
  193. // Release our pointers
  194.  
  195. void CTCEventsDlg::ReleaseApp()
  196. {
  197.     DisconnectEvents();
  198.     if (m_pIApplication != NULL)
  199.     {
  200.         m_pIApplication->Release();
  201.         m_pIApplication = NULL;
  202.     }
  203. }
  204.  
  205. // Connect our app's event sink to the TurboCAD server.
  206. // Connect or start up the TurboCAD server if we need to.
  207.  
  208. void CTCEventsDlg::ConnectEvents()
  209. {
  210.     CString strStr;
  211.     if (m_dwEventConnection != 0)
  212.     {
  213.         AfxMessageBox("Already connected.");
  214.         return;
  215.     }
  216.     if (m_pIApplication == NULL)
  217.     {
  218.         BOOL bCreateInstance = TRUE;
  219.         IUnknown* pUnk = GetActiveObject(&bCreateInstance);
  220.         if (pUnk == NULL)
  221.             return;
  222.  
  223.         HRESULT hRes = pUnk->QueryInterface(IID_IApplication, (void**)&m_pIApplication);
  224.         pUnk->Release();
  225.         
  226.         if (SUCCEEDED(hRes) && m_pIApplication)
  227.         {
  228.             hRes = m_pIApplication->put_Visible (TRUE);
  229.         }
  230.  
  231.         if (FAILED(hRes))
  232.         {
  233.             strStr.LoadString(IDS_TCADINIT_FAILED);
  234.             AfxMessageBox(strStr);   //"Object is not a TurboCAD application!"
  235.             return;
  236.         }
  237.         else
  238.         {
  239.             if (bCreateInstance)
  240.             {
  241.                 strStr.LoadString(IDS_CREATED_NEWTCAD_OBJECT);
  242.                 AfxMessageBox(strStr);//"Created new Application object."
  243.             }
  244.             else
  245.             {
  246.                 strStr.LoadString(IDS_ATTACED_TO_RUNNED_TURBACAD);
  247.                 AfxMessageBox(strStr);//"Attached to already running Application object."
  248.             }
  249.             GetDlgItem(IDC_EXIT_APP)->EnableWindow(TRUE);
  250.         }
  251.     }
  252.  
  253.     IDispatch* pUnkEventSink = theApp.GetIDispatch(TRUE);
  254.  
  255.     UpdateData(TRUE);
  256.     COleVariant var((long)m_dwEventMask);
  257.     HRESULT hRes = m_pIApplication->ConnectEvents(pUnkEventSink,
  258.         &var,
  259.         (long*)&m_dwEventConnection);
  260.     if (FAILED(hRes))
  261.     {
  262.         strStr.LoadString(IDS_CONNECTION_NOT_ESTABLISHED);
  263.         AfxMessageBox(strStr);
  264.         return;
  265.     }
  266.     else
  267.     {
  268.         TCHAR msg[256];
  269.         strStr.LoadString(IDS_CONNECTION_ESTABLISHED);
  270.         wsprintf(msg, strStr, m_dwEventMask);
  271.         AfxMessageBox(msg);
  272.     }
  273.  
  274.     CString strButtonText;
  275.     strButtonText.LoadString(IDS_DISCONNECT);
  276.     GetDlgItem(IDC_CONNECT_EVENTS)->SetWindowText(strButtonText);
  277. }
  278.  
  279. // Release the event connection
  280.  
  281. void CTCEventsDlg::DisconnectEvents()
  282. {
  283.     CString strStr;
  284.     if (m_pIApplication == NULL || m_dwEventConnection == 0)
  285.         return;
  286.  
  287.      HRESULT hRes = m_pIApplication->DisconnectEvents(m_dwEventConnection);
  288.     if (FAILED(hRes))
  289.     {
  290.         strStr.LoadString(IDS_CONNECTION_NOT_BROKEN);
  291.         AfxMessageBox(strStr);
  292.         return;
  293.     }
  294.  
  295.     strStr.LoadString(IDS_CONNECTION_BROKEN);
  296.     AfxMessageBox(strStr);
  297.     m_dwEventConnection = 0;
  298.  
  299.     // DisconnectEvents can be called from destructor after DestroyWindow()...
  300.     if (::IsWindow(m_hWnd))
  301.     {
  302.         CString strButtonText;
  303.         strButtonText.LoadString(IDS_CONNECT);
  304.         GetDlgItem(IDC_CONNECT_EVENTS)->SetWindowText(strButtonText);
  305.     }
  306. }
  307.  
  308. BEGIN_MESSAGE_MAP(CTCEventsDlg, CDialog)
  309.     //{{AFX_MSG_MAP(CTCEventsDlg)
  310.     ON_WM_SYSCOMMAND()
  311.     ON_WM_PAINT()
  312.     ON_WM_QUERYDRAGICON()
  313.     ON_BN_CLICKED(IDC_CONNECT_EVENTS, OnConnectEvents)
  314.     ON_BN_CLICKED(IDC_EXIT_APP, OnExitApp)
  315.     //}}AFX_MSG_MAP
  316.     ON_CONTROL_RANGE(BN_CLICKED, IDC_EVENT1, IDC_EVENT28, OnEventMask)
  317. END_MESSAGE_MAP()
  318.  
  319. /////////////////////////////////////////////////////////////////////////////
  320. // CTCEventsDlg message handlers
  321.  
  322. // TurboCAD SDK: TurboCAD Class Identifier and Interface Identifier
  323. const CLSID CLSID_Application  = {0x6A481001,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
  324. const CLSID CLSID_XApplication = {0x6A481801,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
  325. const IID IID_IApplication = {0x6A481101,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
  326.  
  327. BOOL CTCEventsDlg::OnInitDialog()
  328. {
  329.     CDialog::OnInitDialog();
  330.  
  331.     // Add "About..." menu item to system menu.
  332.  
  333.     // IDM_ABOUTBOX must be in the system command range.
  334.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  335.     ASSERT(IDM_ABOUTBOX < 0xF000);
  336.  
  337.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  338.     CString strAboutMenu;
  339.     strAboutMenu.LoadString(IDS_ABOUTBOX);
  340.     if (!strAboutMenu.IsEmpty())
  341.     {
  342.         pSysMenu->AppendMenu(MF_SEPARATOR);
  343.         pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  344.     }
  345.  
  346.     // Set the icon for this dialog.  The framework does this automatically
  347.     //  when the application's main window is not a dialog
  348.     SetIcon(m_hIcon, TRUE);            // Set big icon
  349.     SetIcon(m_hIcon, FALSE);        // Set small icon
  350.  
  351.     // Enable the buttons
  352.     GetDlgItem(IDC_CONNECT_EVENTS)->EnableWindow(TRUE);
  353.     GetDlgItem(IDC_EXIT_APP)->EnableWindow(FALSE);
  354.  
  355.     m_edit.SubclassDlgItem(IDC_EDIT_LOG, this);
  356.  
  357.     return TRUE;  // return TRUE  unless you set the focus to a control
  358. }
  359.  
  360. void CTCEventsDlg::OnSysCommand(UINT nID, LPARAM lParam)
  361. {
  362.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  363.     {
  364.         CAboutDlg dlgAbout;
  365.         dlgAbout.DoModal();
  366.     }
  367.     else
  368.     {
  369.         CDialog::OnSysCommand(nID, lParam);
  370.     }
  371. }
  372.  
  373. // If you add a minimize button to your dialog, you will need the code below
  374. //  to draw the icon.  For MFC applications using the document/view model,
  375. //  this is automatically done for you by the framework.
  376.  
  377. void CTCEventsDlg::OnPaint()
  378. {
  379.     if (IsIconic())
  380.     {
  381.         CPaintDC dc(this); // device context for painting
  382.  
  383.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  384.  
  385.         // Center icon in client rectangle
  386.         int cxIcon = GetSystemMetrics(SM_CXICON);
  387.         int cyIcon = GetSystemMetrics(SM_CYICON);
  388.         CRect rect;
  389.         GetClientRect(&rect);
  390.         int x = (rect.Width() - cxIcon + 1) / 2;
  391.         int y = (rect.Height() - cyIcon + 1) / 2;
  392.  
  393.         // Draw the icon
  394.         dc.DrawIcon(x, y, m_hIcon);
  395.     }
  396.     else
  397.     {
  398.         CDialog::OnPaint();
  399.     }
  400. }
  401.  
  402. // The system calls this to obtain the cursor to display while the user drags
  403. //  the minimized window.
  404. HCURSOR CTCEventsDlg::OnQueryDragIcon()
  405. {
  406.     return (HCURSOR) m_hIcon;
  407. }
  408.  
  409. void CTCEventsDlg::OnConnectEvents()
  410. {
  411.     if (m_dwEventConnection == 0)
  412.     {
  413.         ConnectEvents();
  414.     }
  415.     else
  416.     {
  417.         DisconnectEvents();
  418.     }
  419. }
  420.  
  421. void CTCEventsDlg::OnExitApp()
  422. {
  423.     if (m_pIApplication == NULL)
  424.     {
  425.         AfxMessageBox("Start application first.");
  426.         return;
  427.     }
  428.     DisconnectEvents();
  429.     m_pIApplication->Quit();
  430.     ReleaseApp();
  431.     GetDlgItem(IDC_EXIT_APP)->EnableWindow(FALSE);
  432. }
  433.  
  434. void CTCEventsDlg::OnEventMask(UINT nIDC)
  435. {
  436.     // Change the connection if we're connected...
  437.     if (m_dwEventConnection != 0)
  438.     {
  439.         DisconnectEvents();
  440.         ConnectEvents();  
  441.     }
  442. }
  443.